﻿using B.S.BasicData.Infrastructure.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.S.BasicData.Infrastructure.Implement
{
    /// <summary>
    /// 仓储接口实现层
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly MyDbContext dbContext;

        public BaseRepository(MyDbContext dbContext)
        {
            this.dbContext = dbContext;
        }
        public int Add(T t)
        {
            dbContext.Add(t);
            return dbContext.SaveChanges();
        }

        public int AddAsnyc(List<T> t)
        {
            dbContext.Set<T>().AddRangeAsync(t);
            return dbContext.SaveChanges();
        }

        public int Del(T t)
        {
            dbContext.Remove(t);
            return dbContext.SaveChanges();
        }

        public int Delete(List<T> id)
        {
            dbContext.Set<T>().RemoveRange(id);
            return dbContext.SaveChanges();
        }

        public IQueryable<T> GetAll()
        {
            return dbContext.Set<T>();
        }

        public T GetValue(int Id)
        {
            return dbContext.Set<T>().Find(Id);
        }

        public int Update(T t)
        {
            dbContext.Update(t);
            return dbContext.SaveChanges();
        }
    }
}
